package com.xxl.dao.impl;

import com.xxl.dao.ClassesDao;
import com.xxl.util.JDBCUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class ClassesDaoImpl implements ClassesDao {
    private Connection conn;
    private PreparedStatement preparedStatement;
    @Override
    public ResultSet getAll() {
        try {
            conn = new JDBCUtil().init();
            preparedStatement = conn.prepareStatement("SELECT classesId,classesName FROM classes");
            return preparedStatement.executeQuery();
        } catch (ClassNotFoundException e) {
            System.out.println(e);
        } catch (SQLException throwables) {
            System.out.println(throwables);
        }
        return null;
    }

    @Override
    public boolean delByClassesId(int classesId) {
        try {

            conn = new JDBCUtil().init();
            preparedStatement = conn.prepareStatement("delete from classes where classesId = ?");
//            int index = 1;
            preparedStatement.setInt(1,classesId);
            return preparedStatement.executeUpdate()>=1?true:false;
        } catch (ClassNotFoundException e) {
            System.out.println(e);
        } catch (SQLException throwables) {
            System.out.println(throwables);
        }finally {
            try {
                conn.close();
            } catch (SQLException throwables) {
                System.out.println(throwables);
            }
        }
        return false;
    }

    @Override
    public boolean add(int classesId, String classesName){
        try {
            conn = new JDBCUtil().init();
            preparedStatement = conn.prepareStatement("insert into classes (classesId,classesName) values (?,?)");
//            int index = 1;
            preparedStatement.setInt(1,classesId);
            preparedStatement.setString(2,classesName);
            return preparedStatement.executeUpdate()>=1?true:false;
        } catch (ClassNotFoundException e) {
            System.out.println(e);
        } catch (SQLException throwables) {
            System.out.println(throwables);
        }finally {
            try {
                conn.close();
            } catch (SQLException throwables) {
                System.out.println(throwables);
            }
        }
        return false;
    }

    @Override
    public ResultSet selByClassesId(int classesId)  {
        try {
            conn = new JDBCUtil().init();
            preparedStatement = conn.prepareStatement("SELECT classesName FROM classes where classesId=?");
//            int index = 1;
            preparedStatement.setInt(1,classesId);
            return preparedStatement.executeQuery();
        } catch (ClassNotFoundException e) {
            System.out.println(e);
        } catch (SQLException throwables) {
            System.out.println(throwables);
        }finally {
            try {
                conn.close();
            } catch (SQLException throwables) {
                System.out.println(throwables);
            }
        }
        return null;
    }

    @Override
    public boolean update(int classesId, String classesName) {
        try {
            conn = new JDBCUtil().init();
            preparedStatement = conn.prepareStatement("update classes set classesName = ? where classesId = ?");
//            int index = 1;
            preparedStatement.setString(1,classesName);
            preparedStatement.setInt(2,classesId);
            return preparedStatement.executeUpdate()>=1?true:false;
        } catch (ClassNotFoundException e) {
            System.out.println(e);
        } catch (SQLException throwables) {
            System.out.println(throwables);
        }finally {
            try {
                conn.close();
            } catch (SQLException throwables) {
                System.out.println(throwables);
            }
        }
        return false;
    }

}
